package cn.gson.springboot.model.service;

import cn.gson.springboot.model.dao.ProductDao;
import cn.gson.springboot.model.dao.ProductDetailDao;
import cn.gson.springboot.model.dao.QuotationDao;
import cn.gson.springboot.model.mapper.QuotationMapper;
import cn.gson.springboot.model.pojos.Product;
import cn.gson.springboot.model.pojos.ProductDetail;
import cn.gson.springboot.model.pojos.Quotation;
import cn.gson.springboot.model.vo.ProductDetailVo;
import cn.gson.springboot.model.vo.QuotationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class QuotationService {
    @Autowired
    QuotationMapper quoMapper;
    @Autowired
    QuotationDao quoDao;
    @Autowired
    ProductDao productDao;
    @Autowired
    ProductDetailDao productDetailDao;

    public List<Quotation> selectEmpQuotation(Integer deptId,Integer empId,Integer filter,Integer vague,String content){
        return quoMapper.selectEmpQuotation(deptId,empId,filter,vague,content);
    }

    public Quotation selectEmpQuotationById(Integer quoId){
        return quoMapper.selectEmpQuotationById(quoId);
    }

    public List<Quotation> selectQuotationByOppId(Integer oppId){
        return quoMapper.selectQuotationByOppId(oppId);
    }

    public Quotation selectQuotationByQuoNo(String quoNo){
        return quoMapper.selectQuotationByQuoNo(quoNo);
    }

    @Transactional(transactionManager = "transactionManager")
    public Integer saveQuotation(QuotationVo vo){
        // 先增主表
        Quotation quo = new Quotation();
        quo.setQuotationId(vo.getQuotationId());
        quo.setQuotationTheme(vo.getQuotationTheme());
        quo.setQuotationNo(vo.getQuotationNo());
        quo.setQuotationStatus(vo.getQuotationStatus());
        quo.setQuotationClassify(vo.getQuotationClassify());
        quo.setQuotationDate(vo.getQuotationDate());
        quo.setQuotationPrice(vo.getQuotationPrice());
        quo.setQuotationDelivery(vo.getQuotationDelivery());
        quo.setQuotationPayment(vo.getQuotationPayment());
        quo.setQuotationPackage(vo.getQuotationPackage());
        quo.setQuotationRemarks(vo.getQuotationRemarks());
        quo.setQuotationContactName(vo.getQuotationContactName());
        quo.setEmployeeByEmpId(vo.getEmployeeByEmpId());
        quo.setQuotationAppStatus(vo.getQuotationAppStatus());
        quo.setEmployeeByAppEmpId(vo.getEmployeeByAppEmpId());
        quo.setOpportunityByOppId(vo.getOpportunity());
        quo.setCustomerByCustomerId(vo.getCustomerByCustomerId());
        quoDao.save(quo);
        //增详单
        List<ProductDetailVo> details = vo.getDetails();
        productDetailDao.deleteByQuId(quo.getQuotationId());
        for (ProductDetailVo d : details){
            //分别创建详单对象
            ProductDetail pd = new ProductDetail();
            pd.setProductDetailId(0);
            pd.setProductDetailRemarks(d.getProductDetailRemarks());
            pd.setProductDetailName(d.getProductDetailName());
            pd.setProductDetailModel(d.getProductDetailModel());
            pd.setProductDetailCount(d.getProductDetailCount());
            pd.setProductDetailPrice(d.getProductDetailPrice());
            pd.setProductDetailMoney(d.getProductDetailMoney());
            pd.setProductDetailTotalMoney(d.getProductDetailTotalMoney());
            pd.setProductByProductId(d.getProductByProductId());
            pd.setProductDetailOutNum(d.getProductDetailOutNum());
            pd.setQuotationByQuotationId(quo);
            productDetailDao.save(pd);
        }
        return quo.getQuotationId();
    }

    @Transactional(transactionManager = "transactionManager")
    public void deleteQuotation(Integer quoId){
        quoDao.deleteById(quoId);
    }
}
